package com.ruoyi.web.controller.goods;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/vip/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 获取商品列表
     */
    @PreAuthorize("@ss.hasPermi('vip:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goods post)
    {
        startPage();
        List<Goods> list = goodsService.selectList(post);
        return getDataTable(list);
    }

//    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('vip:goods:export')")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Goods post)
//    {
//        List<Goods> list = goodsService.selectList(post);
//        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
//        util.exportExcel(response, list, "岗位数据");
//    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('vip:goods:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId)
    {
        return AjaxResult.success(goodsService.selectById(postId));
    }

    /**
     * 商品岗位
     */
    @PreAuthorize("@ss.hasPermi('vip:goods:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Goods post)
    {
        if (UserConstants.NOT_UNIQUE.equals(goodsService.checkNameUnique(post)))
        {
            return AjaxResult.error("新增商品'" + post.getGoodsName() + "'失败，商品名称已存在");
        }
//        else if (UserConstants.NOT_UNIQUE.equals(goodsService.checkPostCodeUnique(post)))
//        {
//            return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
//        }
        post.setCreateBy(getUsername());
        return toAjax(goodsService.insert(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('vip:goods:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Goods goods)
    {
        if (UserConstants.NOT_UNIQUE.equals(goodsService.checkNameUnique(goods)))
        {
            return AjaxResult.error("修改商品'" + goods.getGoodsName() + "'失败，商品名称已存在");
        }
//        else if (UserConstants.NOT_UNIQUE.equals(goodsService.checkPostCodeUnique(post)))
//        {
//            return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
//        }
        goods.setUpdateBy(getUsername());
        return toAjax(goodsService.update(goods));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('vip:goods:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsids}")
    public AjaxResult remove(@PathVariable Long[] goodsids)
    {
        return toAjax(goodsService.deleteByIds(goodsids));
    }

//    /**
//     * 获取岗位选择框列表
//     */
//    @GetMapping("/optionselect")
//    public AjaxResult optionselect()
//    {
//        List<SysPost> posts = goodsService.selectPostAll();
//        return AjaxResult.success(posts);
//    }
}
